export default class Camera {
  static takePictures (cameraId, param) {
    let camera = document.getElementById(cameraId);
    //给拍照对象添加内容改变的监听事件，注意后面不能使用ES6的箭头函数
    camera.addEventListener("change", function () {
      let file = this.files[0];
      //校验拍照文件是否存在
      if (!file || !file.type || !/image\/\w+/.test(file.type)) {
        param.error("未选择图片");
      }
      let disparityData = (new Date()).valueOf() - file.lastModified
      if (disparityData > 120 * 1000) {
        param.error("图片拍摄时间已超时");
      } else {
        var URL = window.URL || window.webkitURL;
        var blob = URL.createObjectURL(file);
        var base64;
        var img = new Image();
        img.src = blob;
        img.onload = function () {
          var that = this;
          //生成比例
          var w = that.width,
            h = that.height,
            scale = w / h;
          w = 400 || w;
          h = w / scale;
          //生成canvas
          var canvas = document.createElement('canvas');
          var ctx = canvas.getContext('2d');
          canvas.width = w
          canvas.height = h
          ctx.drawImage(that, 0, 0, w, h);
          // 生成base64            
          base64 = canvas.toDataURL('image/jpeg', 0.1);
          console.log(base64)
          param.success(base64);
        }
      }


    });
  }
}